package com.zh.note.huawei.onehundred.通信误码;

import java.util.*;

/**
 * @Author: zhanghuan
 * @date: 2023/8/8 10:32
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // int n = sc.nextInt();
        // String line = sc.nextLine();
        int n = 5;
        String line = "1 2 2 4 1";
        int res = solveMehtod(n, line);
        System.out.println(res);

    }

    private static int solveMehtod(int n, String line) {

        int len = Integer.MAX_VALUE;
        if (n == 0) {
            return 0;
        }
        String[] split = line.split(" ");
        int maxCount = 0;
        //存储出现次数最多的，是哪些元素
        Set<Integer> maxCode = new HashSet<>();
        // key是code,value是次数
        Map<Integer, Integer> codeCount = new HashMap<>();
        // key是code,value是它的下标
        Map<Integer, Integer> codeMinIndex = new HashMap<>();//记录元素第一次出现的下标
        Map<Integer, Integer> codeMaxIndex = new HashMap<>();//记录元素最后一次出现的下标

        for (int i = 0; i < split.length; i++) {
            int code = Integer.parseInt(split[i]);

            int count = codeCount.getOrDefault(code, 0) + 1;
            //找到出现次数最大的
            maxCount = Math.max(maxCount, count);
            codeCount.put(code, count);
            if (!codeMinIndex.containsKey(code)) {
                codeMinIndex.put(code, i);
            }
            codeMaxIndex.put(code, i);

        }
        for (Map.Entry<Integer, Integer> entry : codeCount.entrySet()) {
            if (entry.getValue() == maxCount) {
                maxCode.add(entry.getKey());
            }
        }
        for (Integer i : maxCode) {
            int low = codeMinIndex.get(i);
            int high = codeMaxIndex.get(i);
            len = Math.min(len, high - low + 1);

        }
        return len;
    }


}
